Default( "StartingCredits", 5000 )
Default( "DeathPercentLost", 10 )

local NPCBounties = {
	npc_combine_s = 50,
	npc_metropolice = 30,
	npc_zombie = 20,
	npc_hunter = 100,
}

if SERVER then
	Default( "FightEndReward", 1000 )
	
	function GM:InitializePlayerCredits (pl)
		pl:SetValue( "credits", pl:GetValue( "credits", self.StartingCredits ) )
		self:SendCredits (pl)
	end
	
	GM:AddHook( "PlayerInitialSpawn", "InitializePlayerCredits" )
	
	function GM:CreditsForNPCKills (npc, pl, wpn, mul)
		mul = mul or 1
		if !pl:IsPlayer() then return end
		local bounty = (NPCBounties[npc:GetClass()] or 30) * mul
		self:GiveCredits( pl, bounty, npc:GetPos() + Vector (0,0,70) ) --the vector is going to be sent to the client for a "+/-x" popup
		pl:AddFrags (math.ceil(bounty / 25))
	end

	GM:AddHook( "OnNPCKilled", "CreditsForNPCKills" )
	
	function GM:CreditsForBuildRound ()
		if self:IsBuild() then
			for _,pl in pairs (player.GetAll()) do
				self:GiveCredits (pl, self.FightEndReward)
			end
		end
	end
	
	GM:AddHook ( "RoundChanged", "CreditsForBuildRound" )
	
	function GM:GiveCredits( pl, amount, vecPopup )
		--give to player
		pl:SetValue( "credits", pl:GetValue( "credits", 0 ) + amount )
		--give to player weapon
		if not wpn or wpn == pl then
			wpn = pl:GetActiveWeapon() or pl
		end
		
		if ( wpn:IsValid() ) then
			local wpnName = wpn:GetClass() or "unarmed"
			pl:SetValue( "credits_"..wpnName, pl:GetValue( "credits_"..wpnName, 0 ) + amount )
		end
		
		self:SendCredits (pl, wpnName, vecPopup)
	end
	function GM:SpendCredits( ply, amount )
		if ( self:GetCredits( ply ) >= amount ) then
			self:GiveCredits( ply, -amount )
			return true
		end
		return false
	end
	
	function GM:SendCredits (pl, wpnName, vecPopup)
		umsg.Start ("CredUpdate", pl)
		umsg.Long (pl:GetValue( "credits", 0 ))
		if wpnName then
			umsg.Bool (true)
			umsg.String (wpnName)
			umsg.Long (pl:GetValue( "credits_"..wpnName, 0 ))
		else
			umsg.Bool (false)
		end
		if vecPopup then
			umsg.Bool (true)
			umsg.Vector (vecPopup)
		else
			umsg.Bool (false)
		end
		umsg.End ()
	end
	
	function GM:LoseCreditsOnDeath( ply, inflictor, attacker )
		if ( self:IsBuild() ) then return end // No penalty for dying during build.
		self:SpendCredits( ply, self:GetCredits( ply ) * ( self.DeathPercentLost / 100 ) )
	end
	GM:AddHook( "PlayerDeath", "LoseCreditsOnDeath" )
else
	--CLIENTSIDE
	GM.credits = 0
	
	local function RecvCredUpdate (umsg)
		GAMEMODE:ReceiveCreditsUpdate (umsg)
	end
	usermessage.Hook ("CredUpdate", RecvCredUpdate)
	
	GM.weaponcredits = {}
	
	function GM:ReceiveCreditsUpdate (umsg)
		local newCredits = umsg:ReadLong()
		local diff = newCredits - self.credits
		self.credits = newCredits
		if umsg:ReadBool() then
			local str = umsg:ReadString()
			self.weaponcredits[str] = umsg:ReadLong()
		end
		if diff == 0 then return end
		if umsg:ReadBool() then
			local vecPopup = umsg:ReadVector()
			printd (diff.." at "..tostring(vecPopup))
			self:AddCreditPopup (vecPopup, diff) --request player sees a credit change as a popup at a location in the world
		else
			if self.AddCreditPopup then
				self:AddCreditPopup (nil, diff) --request player sees generic pop-down from credits stock
			end
		end
	end
end

function GM:GetCredits (pl)
	if CLIENT then
		return self.credits
	else
		return pl:GetValue( "credits", 0 )
	end
end
